﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows;
using System.Windows.Threading;

namespace Hugo.UIPages
{
    class NewSkypeEndCallUIPage : UIPage
    {
        private string displayName;

        Rectangle reject;
        private int indexOfSelectedImage;
        private System.Media.SoundPlayer sound;
        private DispatcherTimer readyTimer;

        public NewSkypeEndCallUIPage(String displayName) : base("blah",Util.getBitmapSource(Properties.Resources.icon_custom))
        {
            this.displayName = displayName;
            
            Label instructionsLabel = new Label();
            instructionsLabel.Content = Translate.translate("Calling ") + displayName.Trim();
            instructionsLabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            instructionsLabel.Foreground = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.White);
            instructionsLabel.FontSize = GlobalSettings.SelectedLabelFontSize;
            instructionsLabel.Margin = new System.Windows.Thickness(0, GlobalSettings.EffectiveScreenMiddleY-250, 0, 0);

            Grid g = new Grid();
            g.Width = GlobalSettings.ScreenMiddleX * 2;
            PageCanvas.Children.Add(g);

            g.Children.Add(instructionsLabel);

            reject = Util.getSymbol(Util.getBitmapSource(Properties.Resources.calldecline));
            PageCanvas.Children.Add(reject);

            Canvas.SetTop(reject, GlobalSettings.EffectiveScreenMiddleY - 100);
            Canvas.SetLeft(reject, GlobalSettings.ScreenMiddleX - reject.Width/2);

        }

        public override bool respondToClick(System.Windows.Input.MouseButtonEventArgs e)
        {
            if (indexOfSelectedImage != -1)
            {
                // accept
                if (indexOfSelectedImage == 0)
                {
                    endCall();
                }

                return true;
            }

            return false;
        }


        public void setPage()
        {
            checkForHangup();
        }

        private void checkForHangup()
        {
            readyTimer = new DispatcherTimer();
                readyTimer.Interval = TimeSpan.FromMilliseconds(2000);
                readyTimer.Tick += new EventHandler(readyTimer_Tick);
                readyTimer.Start();
        }

        void readyTimer_Tick(object sender, EventArgs e)
        {
            if (!Communicate.mySession.callActive)
            {
                endCall();
            }
        }

        private void endCall()
        {
            Communicate.activeParticipant.hangup();
            Communicate.mySession.callActive = false;
            if (readyTimer != null) { readyTimer.Stop(); }
            MainWindow.popAllPages();
        }

        public override bool setCursorPosition(System.Windows.Point p)
        {
            int ind = getIndexOfImageAtPosition(p);

            if (ind != -1)
            {
                return true;
            }

            return false;
        }

        public int getIndexOfImageAtPosition(System.Windows.Point p)
        {
            int index = -1;

            int i = 0;
            foreach (Rectangle o in new List<Rectangle> { reject })
            {
                Rectangle im = o;

                Point pos = im.PointToScreen(new Point());

                double rX = pos.X - HandCursor.handSize / 2;
                double rY = pos.Y - HandCursor.handSize / 2;
                double rX2 = rX + im.ActualWidth;
                double rY2 = rY + im.ActualHeight;

                if (rX < p.X && rY < p.Y && rX2 > p.X && rY2 > p.Y)
                {
                    index = i;
                    break;
                }
                i++;
            }

            indexOfSelectedImage = index;

            return index;
        }

    }
}
